package com.glory.eapequipmentmonitor.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.fragment.ContactFragment;
import com.glory.eapequipmentmonitor.fragment.IndexFragment;
import com.glory.eapequipmentmonitor.fragment.MineFragment;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabEntity;
import com.pda.platform.ui.ui_pdaplatform.tablayout.CommonTabLayout;
import com.pda.platform.ui.ui_pdaplatform.tablayout.listener.CustomTabEntity;

import java.util.ArrayList;

import butterknife.BindView;

public class HomeActivity extends FreeUI_BaseActivity{
    @BindView(R.id.tabLayout)
    CommonTabLayout tabLayout;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        initPermission();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    private void initPermission() {
        String[] permissions = {
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA
        };

        ArrayList<String> toApplyList = new ArrayList<String>();

        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
                toApplyList.add(perm);
                // 进入到这里代表没有权限.

            }
        }
        String[] tmpList = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
        }
    }

    @Override
    protected void initData() {
        mFragments.add(new IndexFragment());
        mFragments.add(new ContactFragment());
        mFragments.add(new MineFragment());

        Resources resources = getResources();
        mTabEntities.add(new FreeUI_TabEntity("首页",
                resources.getDrawable(R.drawable.home_select), resources.getDrawable(R.drawable.home_normal)));
        mTabEntities.add(new FreeUI_TabEntity("通讯录",
                resources.getDrawable(R.drawable.contact_select), resources.getDrawable(R.drawable.contact_normal)));
        mTabEntities.add(new FreeUI_TabEntity("我的",
                resources.getDrawable(R.drawable.mine_select), resources.getDrawable(R.drawable.mine_normal)));

        tabLayout.setTabData(mTabEntities, this, R.id.content, mFragments);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }
}
